﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace IIControls.Source
{
    /// <summary>
    /// VoltageValve.xaml 的交互逻辑
    /// </summary>
    public partial class VoltageValve : UserControl
    {
        public VoltageValve(CMDType type, ButtonClickEvent click)
        {
            InitializeComponent();
            ButtonType = type;
            BCE = click;
        }

        public VoltageValve(CMDType type)
        {
            InitializeComponent();
            ButtonType = type;
        }

        public VoltageValve()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 按钮功能
        /// </summary>
        public enum CMDType
        {
            /// <summary>
            /// 显示开按钮
            /// </summary>
            VALVE_OPEN,
            /// <summary>
            /// 显示关按钮
            /// </summary>
            VALVE_CLOSE,
            /// <summary>
            /// 显示检测状态按钮
            /// </summary>
            VALVE_CHECKSTATUE,
            /// <summary>
            /// 显示检测电压按钮
            /// </summary>
            VALVE_CHECKVOLTAGE,
            /// <summary>
            /// 显示初始化状态按钮
            /// </summary>
            VALVE_INIT
        }


        private CMDType _ButtonType;
        /// <summary>
        /// 按钮类型
        /// </summary>
        public CMDType ButtonType
        {
            get { return _ButtonType; }
            set
            {
                _ButtonType = value;
                DrawButton();
            }
        }


        private bool _isenable;
        /// <summary>
        /// 该按钮是否按下
        /// </summary>
        public bool ButtonIsEnable
        {
            get { return _isenable; }
            set
            {
                _isenable = value;
                DrawButton();
            }
        }

        /// <summary>
        /// 选择显示的按钮
        /// </summary>
        private void DrawButton()
        {
            HiddenAll();
            switch (ButtonType)
            {
                case CMDType.VALVE_OPEN:
                    if (ButtonIsEnable)
                        ValveOpen.Visibility = Visibility.Visible;
                    else
                        ValveOpenCancel.Visibility = Visibility.Visible;
                    break;
                case CMDType.VALVE_CLOSE:
                    if (ButtonIsEnable)
                        ValveClose .Visibility = Visibility.Visible;
                    else
                        ValveCloseCancel.Visibility = Visibility.Visible;
                    break;
                case CMDType.VALVE_CHECKSTATUE:
                    if (ButtonIsEnable)
                        ValveCheckStatue .Visibility = Visibility.Visible;
                    else
                        ValveCheckStatueCancel .Visibility = Visibility.Visible;
                    break;
                case CMDType.VALVE_CHECKVOLTAGE:
                    if (ButtonIsEnable)
                        ValveCheckVoltage .Visibility = Visibility.Visible;
                    else
                        ValveCheckVoltageCancel .Visibility = Visibility.Visible;
                    break;
                case CMDType.VALVE_INIT:
                     if (ButtonIsEnable)
                        ValveSetUp  .Visibility = Visibility.Visible;
                    else
                        ValveSetUpCancel  .Visibility = Visibility.Visible;
                    break;
            }
        }

        private void HiddenAll()
        {
            ValveOpen.Visibility = Visibility.Hidden ;
            ValveOpenCancel.Visibility = Visibility.Hidden;
            ValveCloseCancel.Visibility = Visibility.Hidden ;
            ValveClose.Visibility = Visibility.Hidden;
            ValveCheckStatue.Visibility = Visibility.Hidden;
            ValveCheckStatueCancel.Visibility = Visibility.Hidden;
            ValveCheckVoltage.Visibility = Visibility.Hidden;
            ValveCheckVoltageCancel.Visibility = Visibility.Hidden;
            ValveSetUp .Visibility = Visibility.Hidden;
            ValveSetUpCancel .Visibility = Visibility.Hidden;
        }

        ButtonClickEvent BCE;
        public delegate void ButtonClickEvent(VoltageValve Valve);

        private ScaleTransform ScaleValue(double value)
        {
            ScaleTransform ST = new ScaleTransform();
            
            ST.ScaleX = value;
            ST.ScaleY = value;
            return ST;
        }

        private void Valve_MouseEnter(object sender, MouseEventArgs e)
        {
            this.RenderTransformOrigin = new Point(0.5, 0.5);
            this.RenderTransform = ScaleValue(1.5);
            Cursor = Cursors.Hand;    
        }

        private void Valve_MouseLeave(object sender, MouseEventArgs e)
        {
            this.RenderTransformOrigin = new Point(0.5, 0.5);
            this.RenderTransform = ScaleValue(1.0);
            Cursor = Cursors.Arrow;
        }

        private void Valve_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            try
            {
                ButtonIsEnable = !ButtonIsEnable;
                BCE(this);
            }
            catch (Exception) { }
           
        }
    }
}
